package ${package.ServiceImpl};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.CaseFormat;
import com.rxw.nutmall.page.PageData;
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
<#if table.serviceInterface>
import ${package.Service}.${table.serviceName};
</#if>
import ${superServiceImplClassPackage};
import lombok.SneakyThrows;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>()<#if table.serviceInterface>, ${table.serviceName}</#if> {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}><#if table.serviceInterface> implements ${table.serviceName}</#if> {

    @Override
    public PageData<${entity}> page(Map<String, Object> params) {
       IPage<${entity}> page = baseDao.selectPage(
              getPage(params, "", true),
              getWrapper(params)
       );
       return getPageData(page,${entity}.class);
    }

    @Override
    public ${entity} get(Long id) {
       return baseDao.selectById(id);
    }

    @Override
    public List<${entity}> getList(${entity} dto) {
       QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
       return baseDao.selectList(wrapper);
    }

    @Override
    public String save(${entity} dto) {
       insert(dto);
       <#list table.fields as field>
       <#if field.keyFlag>
       return dto.get${field.capitalName}().toString();
       </#if>
       </#list>
    }

    @Override
    public void saveList(List<${entity}> list) {
       insertBatch(list);
    }

    @Override
    public void update(${entity} dto) {
       updateById(dto);
    }

    @Override
    public void updateList(List<${entity}> list) {
       updateBatchById(list);
    }

    @Override
    public void remove(Long id) {
       deleteById(id);
    }

    @Override
    public void removeList(List<Long> idList) {
       deleteBatchIds(idList);
    }

    @SneakyThrows
    private QueryWrapper<${entity}> getWrapper(Map<String, Object> params){
        QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
        for (String key : params.keySet()) {
            if (StringUtils.isNotBlank(params.get(key).toString())) {
                Field[] fields = ${entity}.class.getDeclaredFields();
                boolean b = Arrays.stream(fields).anyMatch(field -> field.getName().equals(key));
                if (b) {
                    Field field = ${entity}.class.getDeclaredField(key);
                    Class<?> type = field.getType();
                    String fieldUnderline = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,key);
                    Object value = params.get(key);
                    if (type.getName().endsWith("Long") || type.getName().endsWith("Integer")) {
                        value = NumberUtils.toLong(value.toString());
                    }
                    wrapper.like(fieldUnderline, type.cast(value));
               }
            }
       }
       return wrapper;
    }
 }
</#if>
